﻿using System;
using System.Threading;

namespace UnitTests
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("" + ((args.Length > 0) ? args[0] : ""));
            Console.WriteLine("Testing resetting bit:");
            
            byte before = 0x9D;     //1001 1101
            byte and = 0xF7;        //1111 0111
    
            byte after = (byte) (before & and);

            Console.WriteLine(" before: " + Convert.ToString(before, 2));
            Console.WriteLine(" and:    " + Convert.ToString(and, 2));
            Console.WriteLine(" after:  " + Convert.ToString(after, 2));
            Console.WriteLine("");

            Console.WriteLine("Testing getting bit:");
            before = 0x9D;     //1001 1101
            and = 0x04;        //0000 0100
            after = (byte)(before & and);
            Console.WriteLine("");

            Console.WriteLine(" before: " + Convert.ToString(before, 2));
            Console.WriteLine(" and:    " + Convert.ToString(and, 2));
            Console.WriteLine(" after:  " + Convert.ToString(after, 2));
            if(after == and) Console.WriteLine(" determined bit value is set!");
            else Console.WriteLine(" results differ!");
            Console.WriteLine("");

            before = 0x9D;     //1001 1101
            and = 0x02;        //0000 0010
            after = (byte)(before & and);

            Console.WriteLine(" before: " + Convert.ToString(before, 2));
            Console.WriteLine(" and:    " + Convert.ToString(and, 2));
            Console.WriteLine(" after:  " + Convert.ToString(after, 2));
            if (after != and) Console.WriteLine(" determined bit value is not set!");
            else Console.WriteLine(" results differ!");
            Console.WriteLine("");

            Console.WriteLine("Testing getting MSB & LSB:");
            short word = 0x7FBB;
            byte lsb = (byte) word;
            byte msb = (byte) (word >> 8);
            Console.WriteLine(" before: " + Convert.ToString(word, 2) + " (" + Convert.ToString(word, 16) + ")"); 
            Console.WriteLine(" msb:  " + Convert.ToString(msb, 2) + " (" + Convert.ToString(msb, 16) + ")");
            Console.WriteLine(" lsb:  " + Convert.ToString(lsb, 2) + " (" + Convert.ToString(lsb, 16) + ")");
            Console.WriteLine("");

            Console.WriteLine("Testing relative branching:");
            short pos = 0x000C;
            Console.WriteLine(" old position: " + pos + " (" + Convert.ToString(pos, 16) + ": " + Convert.ToString(pos, 2) + ")");
            byte bra = 0xF9;
            Console.WriteLine(" branch value: " + bra + " (" + Convert.ToString(bra, 16) + ": " + Convert.ToString(bra, 2) + ")");
            lsb = (byte) pos;
            msb = (byte) (pos >> 8);
            Console.WriteLine(" msb:    " + Convert.ToString(msb, 2) + " (" + Convert.ToString(msb, 16) + ")");
            Console.WriteLine(" lsb:    " + Convert.ToString(lsb, 2) + " (" + Convert.ToString(lsb, 16) + ")");
            
            Console.WriteLine(" msb<<8: " + Convert.ToString(msb<<8, 2) + " (" + Convert.ToString(msb, 16) + ")");
            Console.WriteLine(" lsb+bra:" + Convert.ToString((byte) (lsb + bra), 2) + " (" + Convert.ToString((byte) (lsb + bra), 16) + ")");

            Console.WriteLine(" * " + pos + " + " + bra);

            short newpos = (short) ((msb << 8) | (byte) (lsb + bra));
            Console.WriteLine(" new position: " + newpos + " (" + Convert.ToString(newpos, 16) + ")");
            newpos = (short) (((short)(pos >> 8) << 8) | (byte) ((byte)pos + bra));
            Console.WriteLine(" [" + Convert.ToString((((pos >> 8) << 8)), 16) + "]");
            Console.WriteLine(" [" + Convert.ToString((((byte) pos) + bra), 16) + "]");
            Console.WriteLine(" direct calc.: " + newpos + " (" + Convert.ToString(newpos, 16) + ")");
            Console.WriteLine("");
;
            
            Thread.Sleep(100000);



            //before: 1001 1101
            //and:    1111 0111
            //after:  1001 0101
            /*
            
Testing relative branching:
 old position: 12 (c: 1100)
 branch value: 249 (f9: 11111001)
 msb:    0 (0)
 lsb:    1100 (c)
 msb<<8: 0 (0)
 lsb+bra:101 (5)
 * 12 + 249
 new position: 5 (5)
 [0]
 [105]
 direct calc.: 5 (5)
            
             */
        }
    }
}
